package com.starsoft.trade.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.trade.bean.TradeBill;

@Repository
public class TradeBillDao extends BaseDao<TradeBill, Integer>{
	
	public List<TradeBill> getBills(Integer acountId, Date beginDate, Date endDate){
		String hql = "from TradeBill where acountId=?0 and billTime>=?1 and billTime<?2 order by billTime asc";
		return this.find(hql, acountId, DateUtil.floorTime(beginDate), DateUtil.ceilTime(endDate));
	}
	
	public List<TradeBill> getBills(Integer acountId, String secId, Date endDate){
		String hql = "from TradeBill where acountId=?0 and billTime>=?1 and billTime<?2 and secId=?3 order by billTime asc";
		return this.find(hql, acountId, DateUtil.floorTime(endDate), DateUtil.ceilTime(endDate), secId);
	}
	
	public Map<String, Object> pageQuery(PageObject po, Integer acountId, String secId, String billType, Date beginDate, Date endDate) {
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(!StringUtil.isEmpty(acountId)){
			criterions.add(Restrictions.eq("acountId", acountId));
		}
		if(!StringUtil.isEmpty(secId)){
			criterions.add(Restrictions.like("secId", secId, MatchMode.ANYWHERE));
		}
		if(!StringUtil.isEmpty(billType)){
			criterions.add(Restrictions.like("billType", billType, MatchMode.ANYWHERE));
		}
		if(beginDate!=null){
			beginDate = DateUtil.floorTime(beginDate);
			criterions.add(Restrictions.ge("billTime", beginDate));
		}
		if(endDate!=null){
			endDate = DateUtil.ceilTime(endDate);
			criterions.add(Restrictions.le("billTime", endDate));
		}
		return pageQuery(entityClass, po, criterions);
	}
	
}
